#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Item Attoribute

Mikan Python Web Framework
"""

__author__ = "K.Sonohara"
__status__ = "develop"
__version__ = "0.0.0_35"
__date__    = "2013/12/22"
__license__ = 'LGPL'

# ------------------------------------------------------------
## Import Section
# ------------------------------------------------------------
# Python
from operator import attrgetter
if __name__ == '__main__':
	import sys
	sys.path.append(".")
	sys.path.append("src")
import time, datetime

# Mikan
from mikan.item import MIKAN_ITEM
from mikan.util import string_to_sha512

# ------------------------------------------------------------
## Variable Section
# ------------------------------------------------------------

# ------------------------------------------------------------
## Function Section
# ------------------------------------------------------------

def to_stracttime_from_string(string):
	s = string
	if len(s) > 20:
		s = string[0:19]

	try:
		st = time.strptime(s, '%Y/%m/%d %H:%M:%S')
		if st:
			return st
	except:
		pass
	try:
		st = time.strptime(s, '%Y-%m-%d %H:%M:%S')
		if st:
			return st
	except:
		pass
	try:
		st = time.strptime(string, '%Y/%m/%d')
		if st:
			return st
	except:
		pass
	try:
		st = time.strptime(string, '%Y-%m-%d')
		if st:
			return st
	except:
		pass
	try:
		st = time.strptime(string, '%H:%M:%S')
		if st:
			return st
	except:
		pass

	return None

def to_value_from_date(attribute, string):
	if string:
		try:
			st = to_stracttime_from_string(string)
			if st:
				return datetime.datetime(st.tm_year, st.tm_mon, st.tm_mday)
		except:
			pass
	return None

def to_value_from_time(attribute, string):
	if string:
		try:
			st = to_stracttime_from_string(string)
			if st:
				return datetime.datetime(st.tm_year, st.tm_mon, st.tm_mday, hour=st.tm_hour, minute=st.tm_min, second=st.tm_sec)
		except:
			pass
	return None

def to_value_from_timestamp(attribute, string):
	if string:
		try:
			st = to_stracttime_from_string(string)
			if st:
				return datetime.datetime(*st[:6])
		except:
			pass
	return None

def to_value_from_bool(attribute, string):
	if string:
		s = string.lower()
		if s == "true":
			return 1
		elif s == "false":
			return 0
	if string == "0":
		return 0
	if string == "1":
		return 1
	if string == 0:
		return 0
	if string == 1:
		return 1
	return None

def to_value_from_long(attribute, string):
	if string:
		if string == "None":
			return 0
		return long(string)
	return None

def to_value_from_int(attribute, string):
	if string:
		if string == "None":
			return 0
		return int(string)
	return None

def to_value_from_short(attribute, string):
	if string:
		if string == "None":
			return 0
		return int(string)
	return None

def to_value_from_float(attribute, string):
	if string:
		if string == "None":
			return 0
		return float(string)
	return None

def to_value_from_item(attribute, string):
	return None

def to_value_from_password(attribute, string, dohash=True):
	if string and (string != ""):
		if dohash:
			return string_to_sha512(string)
		return string
	return None

def to_value_from_string(attribute, string):
	return string

def to_value_from_list(attribute, value):
	for k, v in attribute.list.list.items():

		if k == value:
			return k
		if k == unicode(value):
			return k
		if v.to_display_value() == unicode(value):
			return k

	return value
#s	return None

def to_value(itemdb, attribute, string, dohash=True, isdynamic=False):
	if (not isdynamic) and attribute.isdynamic:
		return None
	if attribute.list:
		if attribute.list.isentitylist:
			if not attribute.list.isloaded:
				attribute.list.build_list(itemdb)

	result = None
	if attribute.classtype == MIKAN_ITEM.CLASS_TYPE_BOOL:
		result = to_value_from_bool(attribute, string)
	elif attribute.classtype == MIKAN_ITEM.CLASS_TYPE_DATE:
		result = to_value_from_date(attribute, string)
	elif attribute.classtype == MIKAN_ITEM.CLASS_TYPE_TIME:
		result = to_value_from_time(attribute, string)
	elif attribute.classtype == MIKAN_ITEM.CLASS_TYPE_TIMESTAMP:
		result = to_value_from_timestamp(attribute, string)
	elif attribute.classtype == MIKAN_ITEM.CLASS_TYPE_LONG:
		result = to_value_from_long(attribute, string)
	elif attribute.classtype == MIKAN_ITEM.CLASS_TYPE_INT:
		result = to_value_from_int(attribute, string)
	elif attribute.classtype == MIKAN_ITEM.CLASS_TYPE_SHORT:
		result = to_value_from_short(attribute, string)
	elif attribute.classtype == MIKAN_ITEM.CLASS_TYPE_FLOAT:
		result = to_value_from_float(attribute, string)
	elif attribute.classtype == MIKAN_ITEM.CLASS_TYPE_PASSWORD:
		result = to_value_from_password(attribute, string, dohash=True)
	elif attribute.classtype == MIKAN_ITEM.CLASS_TYPE_STRING:
		result = to_value_from_string(attribute, string)
	else:
		result = to_value_from_string(attribute, string)

	if result or (result == False):
		if attribute.list:
			r = to_value_from_list(attribute, result)
			return r
		return result

	return None

def sort_entities(l, values):
	if values == None:
		return None
	if len(values) == 0:
		return None

	if l.isentitylist:
		a = None
		for b in l.list.values():
			a = b.sortattributes
			break

		if a != None:
			ord, d = None, False
			if isinstance(a, list):
				for ord, d in a:
					break
			else:
				ord, d = a

			if ord:
				return sorted(values, key=attrgetter(ord.definename), reverse=d)
	else:
		return sorted(values, key=attrgetter("key"))

	return values

# ------------------------------------------------------------
## Class Section
# ------------------------------------------------------------

# ------------------------------------------------------------
## Main Section
# ------------------------------------------------------------
if __name__ == '__main__':
#	a_date = DateAttribute(readonly=False, unique=False, required=False, dynamic=False, list=None, sortkey=0)
#	a_time = TimeAttribute(readonly=False, unique=False, required=False, dynamic=False, list=None, sortkey=0)
#	a_datetime = TimeStampAttribute(readonly=False, unique=False, required=False, dynamic=False, list=None, sortkey=0)
#	print to_value(a_date, "2012/11/12")
#	print to_value(a_date, "2012/11/12 15:15:15")
#	print to_value(a_date, "2012-11-12")
#	print to_value(a_date, "2012-11-12 15:15:15")
#	print to_value(a_date, "15:15:15")
#	print to_value(a_time, "2012/11/12")
#	print to_value(a_time, "2012/11/12 15:15:15")
#	print to_value(a_time, "2012-11-12")
#	print to_value(a_time, "2012-11-12 15:15:15")
#	print to_value(a_time, "15:15:15")
#	print to_value(a_datetime, "2012/11/12")
#	print to_value(a_datetime, "2012/11/12 15:15:15")
#	print to_value(a_datetime, "2012-11-12")
#	print to_value(a_datetime, "2012-11-12 15:15:15")
#	print to_value(a_datetime, "15:15:15")
#
#	a_bool = BoolAttribute(readonly=False, unique=False, required=False, dynamic=False, list=None, sortkey=0)
#	print to_value(a_bool, "True")
#	print to_value(a_bool, "true")
#	print to_value(a_bool, "TRUE")
#	print to_value(a_bool, "False")
#	print to_value(a_bool, "false")
#	print to_value(a_bool, "FALSE")
#	print to_value(a_bool, "")
#	print to_value(a_bool, None)
	pass

# ------------------------------------------------------------
